package cn.tedu.order.service;

import cn.tedu.order.feign.ItemClient;
import cn.tedu.order.feign.UserClient;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp01.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;

    @Override
    public Order getOrder(String orderId) {
        // TODO：远程调用商品，获取商品列表
        // TODO：远程调用用户，获取用户数据
        log.info("获取订单，orderId="+orderId);
        Order order = new Order();
        order.setId(orderId);
        JsonResult<List<Item>> item = itemClient.getItem(orderId);
        JsonResult<User> user = userClient.getUser(8);
        order.setItems(item.getData());
        order.setUser(user.getData());
//        order.setItems();
//        order.setUser();
        return order;
    }

    @Override
    public void addOrder(Order order) {

        log.info("保存订单:"+order);
        //TODO: 远程调用商品，减少商品库存
        JsonResult<?> jsonResult = itemClient.decreaseNumber(order.getItems());
        log.info("商品"+jsonResult);
        //TODO: 远程调用用户，减少用户积分
        JsonResult<?> score = userClient.getScore(order.getUser().getId(), 1000);
        log.info("积分"+score);

    }
}
